package com.maple.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maple.constant.StatusConstant;
import com.maple.dto.CategoryDTO;
import com.maple.dto.CategoryPageQueryDTO;
import com.maple.entity.Category;
import com.maple.entity.Dish;
import com.maple.enumeration.ExceptionEnum;
import com.maple.exception.BusinessException;
import com.maple.mapper.CategoryMapper;
import com.maple.mapper.DishMapper;
import com.maple.mapper.SetmealMapper;
import com.maple.result.PageResult;
import com.maple.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 新增菜品分类
     * @param categoryDTO
     */
    @Override
    public void save(@RequestBody CategoryDTO categoryDTO) {
    // 克隆对象
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        // 封装属性status
        category.setStatus(StatusConstant.ENABLE);

//        新增数据库对象
        categoryMapper.save(category);

    }

    /**
     * 菜品分类
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
//        启用分页查询
        PageHelper.startPage(
                categoryPageQueryDTO.getPage(),
                categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.page(categoryPageQueryDTO);

//        封装对象
        long total = page.getTotal();
        List<Category> result = page.getResult();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(result);

//        返回结果
        return pageResult;
    }

    /**
     * 根据菜品类型查询
     * @param type
     * @return
     */
    @Override
    public List<Category> queryByType(String type) {
        CategoryPageQueryDTO pageQueryDTO = new CategoryPageQueryDTO();
        pageQueryDTO.setType(Integer.valueOf(type));

//        根据类型查询,封装对象集合
        List<Category> list = categoryMapper.page(pageQueryDTO);

//        返回数据
        return list;
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     */
    @Override
    public void changeStatus(String status, String id) {
        Category category = new Category();
        category.setStatus(Integer.valueOf(status));
        category.setId(Long.valueOf(id));
//        根据id修改数据库中的菜品状态
        categoryMapper.update(category);
    }

    /**
     * 更新菜品数据
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
//        对象克隆
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
//        数据库更新
        categoryMapper.update(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(String id) {
//        根据id查询该分类下是否存在菜品
        List<Dish> list = dishMapper.queryByCategoryId(id);

//        有，抛出异常
        if(list != null && !list.isEmpty()){
            throw new BusinessException(ExceptionEnum.CATEGORY_BE_RELATED_BY_DISH);
        }
//        根据id查询该分类下是否存在套餐
        list = setmealMapper.queryByCategoryId(id);

//        有，抛出异常
        if(list != null && !list.isEmpty()){
            throw new BusinessException(ExceptionEnum.DISH_BE_RELATED_BY_SETMEAL);
        }
//        无，删除数据库中的分类
        categoryMapper.deleteById(id);

    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }

}
